﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using GD.ModelContest.Model;
using GD.ModelContest.Service;
using GD.ModelContest.Common;

namespace GD.ModelContest.Web.Admin
{
    public partial class EditNews : BaseCheckPage
    {
        int NewsId = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            //转换获取的参数
            int.TryParse(Request.QueryString["Id"], out NewsId);

            if (!Page.IsPostBack)
            {
                DataBind_News(NewsId);
            }
        }

        protected void DataBind_News(int newsId)
        {
            mc_news article = ServiceLocator.INewsService.GetNewsById(newsId);
            if (article != null)
            {
                this.ddlNewsType.SelectedValue = article.newsTypeId.ToString();
                this.txtTitle.Text = article.title;
                this.txtContent.Text = article.content;
            }
        }

        protected void lkbEdit_Click(object sender, EventArgs e)
        {
            mc_news article = ServiceLocator.INewsService.GetNewsById(NewsId);
            if (article != null)
            {
                article.newsTypeId = Convert.ToInt32(this.ddlNewsType.SelectedValue);
                article.title = this.txtTitle.Text;
                article.content = this.txtContent.Text;

                if (ServiceLocator.INewsService.UpdateNews(article))
                {
                    JavaScript.Alert("恭喜您！修改成功！", this.Page);
                }
                else
                {
                    JavaScript.Alert("对不起！修改失败！", this.Page);
                }
            }
        }
    }
}
